package com.aloda.ojuser.manager;

import com.aloda.ojcommon.constant.ExceptionConstant;
import com.aloda.ojcommon.exception.BaseException;
import com.aloda.ojuser.properties.MailProperties;
import com.aloda.ojuser.properties.VerifyProperties;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

@Component
public class MailManager {

    @Resource
    private MailProperties mailProperties;

    @Resource
    private VerifyProperties verifyProperties;

    /**
     * 发送邮件
     * @param to 收件人邮箱
     * @param text 邮件正文
     * @param title 标题
     */
    public boolean sendMail(String to, String text, String title){
        try {
            final Properties props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.host", mailProperties.getHost());
            // 发件人的账号
            props.put("mail.user", mailProperties.getUsername());
            //发件人的授权码
            props.put("mail.password", mailProperties.getPassword());
            // 启用 SSL
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.port", "465");
            // 设置超时时间
            props.put("mail.smtp.connectiontimeout", "10000");
            props.put("mail.smtp.timeout", "10000");
            // 构建授权信息，用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // 使用环境属性和授权信息，创建邮件会话
            Session mailSession = Session.getInstance(props, authenticator);
            // 创建邮件消息
            MimeMessage message = new MimeMessage(mailSession);
            // 设置发件人
            String username = props.getProperty("mail.user");
            InternetAddress form = new InternetAddress(username);
            message.setFrom(form);

            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 设置邮件标题
            message.setSubject(title);

            // 设置邮件的内容体
            message.setContent(text, "text/html;charset=UTF-8");
            // 发送邮件
            Transport.send(message);
            return true;
        }catch (Exception e){
            throw new BaseException(ExceptionConstant.EMAIL_SEND_ERROR);
        }
    }

    /**
     * 发送验证码邮件
     * @param to 收件人邮箱
     * @param verificationCode 验证码
     */
    public boolean sendVerificationCodeMail(String to, String verificationCode) {
        String time = verifyProperties.getEmailVerifyTimeout().toString();
        String title = mailProperties.getTitle();
        // 使用HTML格式编写邮件内容，添加一些简单的样式
        String text = "<html><body style='font-family: Arial, sans-serif;'>" +
                "<h2>您好！</h2>" +
                "<p>您的验证码是：<span style='font-size: 24px; color: #007BFF;'>" + verificationCode + "</span></p>" +
                "<p>请在有效期("+time+"分钟)内使用该验证码，如非本人操作，请忽略此邮件。</p>" +
                "</body></html>";
        return sendMail(to, text, title);
    }
}
